#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <cstdlib>
#include <arpa/inet.h>

using namespace std;

#define SERVER_PORT 9999
#define BUFFER_SIZE 128


int main()
{
    /* 创建套接字的句柄 */
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd == -1)
    {
        perror("socket error:");
        exit(-1);
    }
    /* 绑定IP和端口信息 */
    struct sockaddr_in localAddress;
    /* 清空结构体的脏数据 */
    memset(&localAddress, 0, sizeof(localAddress));
    /* 地址族设置为IPV4 */
    localAddress.sin_family = AF_INET;
    /* 把主机字节序转为网络 */
    localAddress.sin_port = htons(SERVER_PORT);
    /* 设置全通规则 */
    localAddress.sin_addr.s_addr = htonl(INADDR_ANY);
    bind(sockfd, reinterpret_cast<struct sockaddr*>(&localAddress), sizeof(localAddress));


    char buffer[BUFFER_SIZE] = {0};
    while(1)
    {
        recvfrom(sockfd, buffer, BUFFER_SIZE, 0, NULL, NULL);
        cout << "buffer:" << buffer << endl;
        memset(buffer, 0, sizeof(buffer));
    }

    return 0;
}